close all
clear
warning off

dist_BS_half = 1;
loc_BS_ATG = [-1,0,10/1000];
loc_airplane = [3,0.5,10];
% loc_airplane = [8,-3,10;1,3,10];
% loc_airplane = [80,-30,10;10,30,10];
% loc_airplane_start = [80,-30,10];
% loc_airplane_terminus = [10,30,10];

% 六边形的边长数量 + 1
num_terres_BS_layer=1;
num_terres_BS_ = 0;
for ii = 1:num_terres_BS_layer
    num_terres_BS_ = num_terres_BS_ + (num_terres_BS_layer+1)*2-1;
end
xx=zeros(num_terres_BS_layer,num_terres_BS_layer+1);
yy=zeros(num_terres_BS_layer,num_terres_BS_layer+1);
for i=1:num_terres_BS_layer
    for j=1:i+1
        xx(i,j)=2*i-(j-1);
        yy(i,j)=(j-1)*sqrt(3);
    end
end
xx=reshape(xx,1,(num_terres_BS_layer+1)*num_terres_BS_layer)*dist_BS_half;
yy=reshape(yy,1,(num_terres_BS_layer+1)*num_terres_BS_layer)*dist_BS_half;
xx = [xx,0];
yy = [yy,0];
% loc_BS = [xx,yy];
% loc_BS = unique(loc_BS,'rows');
% plot(xx,yy,'x')

[theta,rou]=cart2pol(xx,yy);
theta1=theta+1*2*pi/6;
theta2=theta+2*2*pi/6;
theta3=theta+3*2*pi/6;
theta4=theta+4*2*pi/6;
theta5=theta+5*2*pi/6;
theta=[theta,theta1,theta2,theta3,theta4,theta5];
% theta=[theta,theta+5*2*pi/6];
rou=[rou,rou,rou,rou,rou,rou];
[xxx,yyy]=pol2cart(theta,rou);


xxx = xxx+num_terres_BS_layer*2*dist_BS_half;
loc_BS = [xxx',yyy',0*ones(length(xxx),1)];
tol = 0.05;
loc_BS = uniquetol(loc_BS,tol,'ByRows',1);
% ind_ATG = abs(loc_BS(:,1)) <= 1e-10 & abs(loc_BS(:,2)) <= 1e-10;
% loc_BS(ind_ATG,:) = [];

dist_BS_user = dist_BS_half;
user_per_cell = 3;
loc_user_ = [];
for ii_user = 1:user_per_cell
    loc_user_ii = loc_user_generate(loc_BS,dist_BS_user);
    loc_user_ = [loc_user_;loc_user_ii'];
end
loc_user = reshape(loc_user_,2,length(loc_BS)*user_per_cell)';
loc_user = [loc_user,0*ones(length(loc_user),1)];
% rand_1 = [1:5;6:10]';
% rand_2 = [11:15;16:20]';
% rand_3 = [21:25;26:30]';
% rand = reshape([rand_1';rand_2';rand_3'],2,length(rand_1)*3)'


% loc_BS_all = loc_BS_all_generate(xxx,yyy,tol);

N=length(loc_BS);
K=user_per_cell;
save('location_1202.mat','N','K','dist_BS_half','dist_BS_user','num_terres_BS_layer',...
    'loc_BS_ATG','loc_airplane','loc_BS','loc_user');
plot_loca(loc_BS_ATG,loc_airplane,loc_BS,loc_user,num_terres_BS_layer,dist_BS_half);







function loc_user = loc_user_generate(loc_BS,dist_BS_user)
num_BS = length(loc_BS);
theta_user = rand(num_BS,1)*2*pi;
rou_user = rand(num_BS,1)*dist_BS_user;
[xxx_user,yyy_user]=pol2cart(theta_user',rou_user');
% [xxx_user,yyy_user]=pol2cart(theta_BS+theta_user',rou_BS+rou_user');
loc_user = [loc_BS(:,1)+xxx_user',loc_BS(:,2)+yyy_user'];
end

function loc_BS_all = loc_BS_all_generate(xxx,yyy,tol)
[theta,rou]=cart2pol(xxx,yyy);
theta1=theta+1*2*pi/3;
theta2=theta+2*2*pi/3;
theta=[theta,theta1,theta2];
rou=[rou,rou,rou];
[xxx,yyy]=pol2cart(theta,rou);
loc_BS_all = [xxx',yyy'];
loc_BS_all = uniquetol(loc_BS_all,tol,'ByRows',1);
% find(loc_BS(:,1) == 0 && loc_BS(:,2) == 0)
ind_ATG = abs(loc_BS_all(:,1)) <= 1e-10 & abs(loc_BS_all(:,2)) <= 1e-10;
loc_BS_all(ind_ATG,:) = [];
end


